#include <REGX52.H>
#include "uart.c"
#include "timer.c"
#include "game.h"
#include "display.c"
/**
 * 实现形状显示
 */
unsigned char type = 1;
unsigned char direction = 0;
unsigned int shape_data;

// 十六个元素，每个代表一行，每行10列，
// 0010 0000 0000 0100 ,中间10位0，每位代表一个4x4像素的格子
unsigned int code GAME_ZONE_INIT_DATA[] = {
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004, //
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004,
    // 多出来3行用来防止数组越界，保持处理逻辑一致
    // 形状是4x4方格，最左上角是坐标原点，允许坐标落到游戏区域的最底端，这时候向下伸出了3行
    0xFFFF, 0xFFFF, 0xFFFF};

unsigned int idata GAME_ZONE[19];

void game_zone_init()
{
    unsigned char i;
    for (i = 0; i < 19; i++)
    {
        GAME_ZONE[i] = GAME_ZONE_INIT_DATA[i];
    }
}

void display_game_zone()
{
    unsigned char i, j;
    bit top;
    bit bottom;
    unsigned int temp;
    unsigned char display_byte[] = {0, 0, 0, 0};

    // 16行数据，每2行数据同时写入，一共做8次
    // i刚好是页数
    for (i = 0; i < 8; i++)
    {
        // 每行10列，1列显示4byte数据
        for (j = 0; j < 10; j++)
        {
            // xx10 0000 0000 01xx
            // GAME_ZONE[i]
            temp = GAME_ZONE[i * 2];
            top = ((temp >> (12 - j)) & 0x0001) == 1;
            temp = GAME_ZONE[i * 2 + 1];
            bottom = ((temp >> (12 - j)) & 0x0001) == 1;
            fill_display_block_arr(top, bottom, display_byte);
            // 30是竖线
            lcd_display_bytes_in_page(i, 31 + 4 * j, display_byte, 4);
            // GAME_ZONE[i+1]
        }
    }
}

void timer0_ISR() interrupt 1
{
    display_game_zone();
}

void main()
{
    uart_init();
    timer0_init();
    display_init();
    game_zone_init();
    GAME_ZONE[0] = 0x2F04;
    GAME_ZONE[4] = 0x3FFC;
    timer0_ISR_enable();
    while (1)
    {
    }
}